package com.hzh.cusviewdemo.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.hzh.cusviewdemo.Adapter.CommentAdapter;
import com.hzh.cusviewdemo.Base.BaseActivity;
import com.hzh.cusviewdemo.Base.LinearOffsetsItemDecoration;
import com.hzh.cusviewdemo.Presenter.MusicCommentData;
import com.hzh.cusviewdemo.Presenter.MusicInfoData;
import com.hzh.cusviewdemo.Presenter.MusicPresenter;
import com.hzh.cusviewdemo.R;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class LookCommentActivity extends BaseActivity implements MusicPresenter, View.OnClickListener {

    public static final String INTENT_TAG_MUSIC_ID = "TAG_MUSIC_ID";
    private String musicId;
    private int page = 1; // 页码 1 ++

    private ImageView alc_back;
    private TextView alc_txt_title;
    private SmartRefreshLayout alc_smart;
    private RecyclerView alc_list_comment;
    private LinearLayout alc_linear_no_data;

    private CommentAdapter commentAdapter;
    private List<MusicCommentData.CommentData> dataList = new ArrayList<>();

    @Override
    public int getLayout() {
        return R.layout.activity_look_comment;
    }

    @Override
    public void initView() {
        alc_back = findViewById(R.id.alc_back);
        alc_smart = findViewById(R.id.alc_smart);
        alc_txt_title = findViewById(R.id.alc_txt_title);
        alc_list_comment = findViewById(R.id.alc_list_comment);
        alc_linear_no_data = findViewById(R.id.alc_linear_no_data);

        alc_list_comment.setNestedScrollingEnabled(false);
        LinearOffsetsItemDecoration linearOffsetsItemDecoration = new LinearOffsetsItemDecoration(LinearOffsetsItemDecoration.LINEAR_OFFSETS_VERTICAL);
        linearOffsetsItemDecoration.setItemOffsets(20);
        alc_list_comment.addItemDecoration(linearOffsetsItemDecoration);

        // 加载资源列表
        alc_smart.setRefreshHeader(new ClassicsHeader(this));
        alc_smart.setRefreshFooter(new ClassicsFooter(this));
        alc_smart.setOnRefreshListener(refreshLayout -> {
            initMusicCommentList();
        });
        alc_smart.setOnLoadMoreListener(refreshlayout -> {
            page++;
            LoadMoreComment();
        });

        alc_back.setOnClickListener(this);

    }

    /**
     * 加载更多评论
     */
    private void LoadMoreComment() {
        page++;
        loadMore();
    }

    private void loadMore() {
        getMusicComment(musicId.contains("MUSIC") ? musicId.substring(musicId.indexOf("_") + 1) : musicId, page, new OnMusicCommentListener() {
            @Override
            public void onGetCommentSuccess(MusicCommentData musicCommentData) {
                alc_txt_title.setText("歌曲评论(" + musicCommentData.getTotal() + ")");
                if (musicCommentData.getRows() == null) {
                    ToastUtils.showShort("没有更多数据");
                    alc_smart.finishLoadMore();
                    return;
                }
                dataList.addAll(musicCommentData.getRows());
                if (dataList.size() > 0) {
                    showDataOrNull(true);
                    commentAdapter = new CommentAdapter(dataList, LookCommentActivity.this);
                    alc_list_comment.setLayoutManager(new LinearLayoutManager(LookCommentActivity.this));
                    alc_list_comment.setAdapter(commentAdapter);
                }
                alc_smart.finishLoadMore();
            }

            @Override
            public void onGetCommentFail(String msg) {
                ToastUtils.showShort(msg);
                alc_smart.finishLoadMore();
            }
        });
    }

    /**
     * 加载评论列表
     */
    private void initMusicCommentList() {
        showLoading(this);
        getMusicComment(musicId.contains("MUSIC") ? musicId.substring(musicId.indexOf("_") + 1) : musicId, page, new OnMusicCommentListener() {
            @Override
            public void onGetCommentSuccess(MusicCommentData musicCommentData) {
                alc_txt_title.setText("歌曲评论(" + musicCommentData.getTotal() + ")");
                dataList = musicCommentData.getRows();
                if (dataList.size() > 0) {
                    showDataOrNull(true);
                    commentAdapter = new CommentAdapter(dataList, LookCommentActivity.this);
                    alc_list_comment.setLayoutManager(new LinearLayoutManager(LookCommentActivity.this));
                    alc_list_comment.setAdapter(commentAdapter);
                } else {
                    showDataOrNull(false);
                    if (commentAdapter != null) {
                        commentAdapter.clearData();
                    }
                }
                alc_smart.finishRefresh();
                showComplete();
            }

            @Override
            public void onGetCommentFail(String msg) {
                ToastUtils.showShort(msg);
                showComplete();
                alc_smart.finishRefresh();
            }
        });
    }

    private void showDataOrNull(boolean isShowData) {
        runOnUiThread(() -> {
            if (isShowData) {
                alc_list_comment.setVisibility(View.VISIBLE);
                alc_linear_no_data.setVisibility(View.GONE);
            } else {
                alc_list_comment.setVisibility(View.GONE);
                alc_linear_no_data.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void initData() {
        musicId = getIntent().getStringExtra(INTENT_TAG_MUSIC_ID);
        initMusicCommentList();
    }

    @Override
    public void onClick(View view) {
        if (view == alc_back) {
            finish();
        }
    }
}